<template>
  <div class="inventoryPosting">
    <div class="header">
      <div class="header-name">
        <span>{{ $t('market.fdate') }}</span>
        <el-date-picker
          v-model="fdate"
          type="month"
          value-format="yyyy-MM-dd"
          size="mini"
          style="width: 152px"
        />
        <span>{{ $t('market.deptClassify') }}</span><el-input v-model.trim="workShopName" size="mini" style="width: 180px" />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
        <!--        <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />-->
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
        :get-summaries="getSummaries"
        show-summary
      >
        <template v-slot:billSlot="col">
          <el-link type="primary" @click="handleInto(col.scope.row)">
            {{ col.scope.row.物料编码 }}
          </el-link>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  queryStockDetailsList
} from '@/api/financial/salesMargin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import searData from '@/components/Search/mixin'

export default {
  name: 'OtherDetail',
  mixins: [searData],
  data() {
    return {
      fdate: '',
      workShopName: '',
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('market.category'), prop: '存货类型', width: '100' },
        { label: this.$t('market.period'), prop: '期间', width: '60' },
        { label: this.$t('market.stockname'), prop: '仓库名称', width: '100' },
        { label: this.$t('market.deptClassify'), prop: '部门分类' },
        { label: this.$t('market.billType'), prop: '单据类型' },
        { label: this.$t('market.coding'), prop: 'FROMBILLNO' },
        { label: this.$t('market.postingDate'), prop: '过账日期' },
        { label: this.$t('market.deaprt'), prop: '部门' },
        { label: this.$t('market.fmaterial'), prop: '物料编码', type: 'bill', width: '120' },
        { label: this.$t('shop.fname2'), prop: '物料名称', width: '100' },
        { label: this.$t('market.beginNum'), prop: '期初数量', width: '80' },
        { label: this.$t('market.beginDanjia'), prop: '期初单价', width: '80' },
        { label: this.$t('market.beginPrice'), prop: '期初金额', width: '80' },
        { label: this.$t('market.rukufqty'), prop: 'INQTY', width: '80' },
        { label: this.$t('market.instockPrice'), prop: 'INSINGLEWORTH', width: '80' },
        { label: this.$t('market.inallworth'), prop: 'INALLWORTH', width: '80' },
        { label: this.$t('market.outqty'), prop: 'OUTQTY', width: '80' },
        { label: this.$t('market.outPrice'), prop: 'OUTSINGLEWORTH', width: '80' },
        { label: this.$t('market.outAmount'), prop: 'OUTALLWORTH', width: '80' },
        { label: this.$t('market.resultqty'), prop: 'RESULTQTY', width: '80' },
        { label: this.$t('market.resultPrice'), prop: 'RESULTSINGLEWORTH', width: '80' },
        { label: this.$t('market.resultallworth'), prop: 'RESULTALLWORTH', width: '80' }
      ],
      tableHeader1: []
    }
  },
  created() {
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryStockDetailsList({
            date: this.fdate,
            WorkShopName: this.workShopName,
            pageSize: 100000,
            pageNum: 1
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '材料进销存明细表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取财务报表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        date: this.fdate,
        WorkShopName: this.workShopName,
        pageNum: this.currentPage,
        pageSize: this.size
        // search: this.searCollData.concat(this.details)
      }
      const { data: RES } = await queryStockDetailsList(DATA)
      this.tableData = RES.array
      this.total = RES.total
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    handleInto(ev) {
      this.$router.push({ path: `/detailMateriel/${ev.物料编码}` })
    }
  }
}
</script>
<style lang="scss" scoped>
.inventoryPosting {
  @include listBom;
  padding: 0;
  .header{
    position:relative;
    .header-name{
      width: 100%;
      span {
        width: 100px;
        font-size: 12px;
        text-align: center;
      }
    }
    .btn{
      transform: translateY(18%);
      margin-left: 15px !important;
    }
  }
}
</style>
